/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.sys.service;

import com.thinkgem.jeesite.modules.sys.dao.CommunityMapper;
import com.thinkgem.jeesite.modules.sys.dao.ShopAddDao;
import com.thinkgem.jeesite.modules.sys.dao.TbCommunityMapper;
import com.thinkgem.jeesite.modules.sys.dao.UserDao;
import com.thinkgem.jeesite.modules.sys.entity.TbCommunity;
import com.thinkgem.jeesite.modules.sys.entity.TbUserTenementInfo;
import com.thinkgem.jeesite.modules.sys.entity.User;
import com.thinkgem.jeesite.modules.sys.entity.UserArea;
import com.thinkgem.jeesite.modules.sys.utils.UserUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 店铺Service
 * @author ThinkGem
 * @version 2014-05-16
 */
@Service
@Transactional(readOnly = true)
public class CommunityService {
	@Autowired
	private CommunityMapper communityMapper;
	@Autowired
	private UserDao userDao;
	@Autowired
	private ShopAddDao shopAddDao;
	@Autowired
	private TbCommunityMapper tbCommunityMapper;
	@Transactional(readOnly = false)
	public TbCommunity insertCommunity(TbCommunity tbCommunity ,List<TbUserTenementInfo> list) {
		User user = UserUtils.getUser();
		tbCommunity.setCityName(user.getCitys());
		tbCommunity.setAreaName(user.getAreas());
		tbCommunity.setProvinceName(user.getProvinces());
		tbCommunity.setCommName(list.get(0).getCellname());
		user = userDao.findUserByUid(user);
		tbCommunity.setProvinceId(Integer.valueOf(user.getProvinces()));
		tbCommunity.setCityId(Integer.valueOf(user.getCitys()));
		//tbCommunity.setName("物业费");
		TbCommunity community = tbCommunityMapper.getCommitByCommName(tbCommunity);
		if(community==null){
			communityMapper.insertCommunity(tbCommunity);
		}
		community = communityMapper.findAdress(tbCommunity);
		return  community;
	}
	//代理商查询该小区是否存在
	@Transactional(readOnly = false)
	public TbCommunity insertagentCommunity(List<TbUserTenementInfo> list) {
		TbCommunity tbCommunity=new TbCommunity();
		tbCommunity.setCommName(list.get(0).getCellname());
		User user = UserUtils.getUser();
		user = shopAddDao.userAreas(user.getId());
		tbCommunity.setProvinceId(Integer.valueOf(user.getProvinces()));
		tbCommunity.setCityId(Integer.valueOf(user.getCitys()));
		//tbCommunity.setName("物业费");
		TbCommunity community =tbCommunityMapper.getCommitByCommName(tbCommunity);
		return  community;
	}
	//物业管理员导入信息时判断该小区是否存在
	@Transactional(readOnly = false)
	public TbCommunity insertManageCommunity(List<TbUserTenementInfo> list) {
		TbCommunity tbCommunity=new TbCommunity();
		User user = UserUtils.getUser();
		tbCommunity.setCityName(user.getCitys());
		tbCommunity.setAreaName(user.getAreas());
		tbCommunity.setProvinceName(user.getProvinces());
		tbCommunity.setCommName(list.get(0).getCellname());
		user = shopAddDao.userAreas(user.getId());
		tbCommunity.setProvinceId(Integer.valueOf(user.getProvinces()));
		tbCommunity.setCityId(Integer.valueOf(user.getCitys()));
		tbCommunity.setAreaId(Integer.valueOf(user.getAreas()));
		//tbCommunity.setName("物业费");
		//根据小区名字查询该小区是否存在，
		TbCommunity community = tbCommunityMapper.agentCheckCommName(tbCommunity);
		return  community;
	}
	public UserArea getAreaByUserId(UserArea userArea){
		return userDao.getAreaByUserId(userArea);
	}
   /* @Transactional(readOnly = false)
	public TbShop updatePackingFee(TbShop shop) {
		tbShopMapper.updatePackingFee(shop);
		return shop;
	}
	@Transactional(readOnly = true)
	public Page<bMpWalletStream> findWalletStreamPage(Page<TbMpWalletStream> page, TbMpWalletStream walletStream) {

		// 设置默认时间范围，默认当前月
		if (walletStream.getBeginDate() == null){
			walletStream.setBeginDate(DateUtils.setDays(DateUtils.parseDate(DateUtils.getDate()), 1));
		}
		if (walletStream.getEndDate() == null){
			walletStream.setEndDate(DateUtils.addMonths(walletStream.getBeginDate(), 1));
		}

		return super.findPage(page, walletStream);

	}*/
	/*@Transactional(readOnly = false)
	public void delete(Dict dict) {
		super.delete(dict);
		CacheUtils.remove(DictUtils.CACHE_DICT_MAP);
	}*/
}
